package com.qdxwx.web.controller.user;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.course.CourseNoteService;
import com.qdxwx.models.course.CourseNote;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/user/course")
public class UserCourseNoteController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(UserCourseNoteController.class);
    @Autowired
    private CourseNoteService courseNoteService;

    /**
     * 个人中心笔记
     */
    @RequestMapping("/note.html")
    public ModelAndView courseNote(HttpServletRequest request, ModelAndView model) {
        int currentPage = TKConvert.toInteger(request.getParameter("page"));
        model.setViewName(getViewPath("ucenter/uc-course-note"));
        PageRequest page = new PageRequest();
        page.setCurrentPage(currentPage);
        page.setPageSize(4);
        CourseNote courseNote = new CourseNote();
        courseNote.setUserId(LoginUser.getUserId(request));
        int total = courseNoteService.count(courseNote);
        request.setAttribute("totalPages", page.getTotalPages(total));
        List<CourseNote> courseNoteList = courseNoteService.getList(courseNote, page);
        request.setAttribute("page", page);
        request.setAttribute("courseNoteList", courseNoteList);
        return model;
    }

    /**
     * 删除笔记
     */
    @RequestMapping("/note/delete.json")
    @ResponseBody
    public AjaxResult delNote(HttpServletRequest request) {
        long id = Long.parseLong(request.getParameter("id"));
        courseNoteService.delete(id);
        return AjaxResult.success("删除成功");
    }
}